{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "692b74ef-3cd4-47eb-8e21-8f1dff062bda",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9443c28-7eb7-43af-b0d0-70b6a63c61a3",
   "metadata": {},
   "source": [
    " 创建数据集 x,y  \n",
    " 模拟的数据x满足给的y=f(x),预测结果的参数和f很接近，那么就是可用的预测结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a8779da-a407-44c9-9820-17ded6467397",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=2*np.random.rand(100,1)\n",
    "y=4+3*x+np.random.randn(100,1)\n",
    "x_b=np.c_[np.ones((100,1)),x]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e071743-d575-4a65-896b-461d2348365e",
   "metadata": {},
   "source": [
    "创建超参数，和模型的参数名字上进行区分  \n",
    "全量梯度下降，每个批次都用全部数据，单层for跑10000轮次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6da718a6-6ea1-4074-91d5-4317083f5687",
   "metadata": {},
   "outputs": [],
   "source": [
    "n_epochs=10000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f5cbb24-d349-4e95-b4cb-b955c8b005c8",
   "metadata": {},
   "source": [
    "\n",
    "初始化theta,本例就w0,w1两个系数,标准正态分布创建  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7ad88193-6719-4428-bd13-8ed51c33566e",
   "metadata": {},
   "outputs": [],
   "source": [
    "theta=np.random.randn(2,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f43dd4f-4889-43e1-a55a-8f79402418a2",
   "metadata": {},
   "source": [
    "计算梯度gj=(y^-y)*xj    \n",
    "直接x_b转置乘loss就是梯度向量  \n",
    "2行100列  (x_b点乘theta - y ) 100行2两列 2行1列 100行1列  结果就是列向量（g0,g1) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "debeba9e-8edc-44fc-aaa1-a59f0e233c30",
   "metadata": {},
   "source": [
    "wjt+1=wj-n*g  直接向量一步计算完   \n",
    "\n",
    "for 函数循环判断是否收敛，一般不设置阈值，而是满足相对较大的迭代轮次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cf8045f2-9a13-4877-8e04-704fb9f9f9f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "t0,t1=1,200\n",
    "def learning_rate_adjust(t):\n",
    "    return t0/(t+t1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04572d65-b62a-4364-b44c-86bfce123be1",
   "metadata": {},
   "source": [
    "实现动态调整学习因子，即学习率随着迭代次数增大而逐渐减小，让其接近最优解时更精细  \n",
    "t0,t1=1,200 等价（t0,t1）=（1,200）元组类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a891386a-3987-4fdd-b410-3ef05a6d842d",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in range(n_epochs):\n",
    "    gradients=x_b.T.dot(x_b.dot(theta)-y)\n",
    "    learning_rate=learning_rate_adjust(t)\n",
    "    theta=theta-learning_rate * gradients"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54fe18fd-7e6d-48c2-9010-d5609813012c",
   "metadata": {},
   "source": [
    "\n",
    "打印预测参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0b98c1bb-23c6-41c5-b683-30c53eb67830",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.95883404]\n",
      " [3.09751776]]\n"
     ]
    }
   ],
   "source": [
    "print(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2acacb9f-1cb1-4493-aae2-0ecf841ba971",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
